<template>
  <div class="assessments-manage">
    <div class="table-header">
      <div class="left">
        <el-input
          v-model="searchQuery"
          placeholder="搜索用户名"
          style="width: 200px"
          clearable
          @clear="handleSearch"
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        
        <el-select v-model="scaleId" placeholder="选择量表" clearable>
          <el-option
            v-for="scale in scales"
            :key="scale.id"
            :label="scale.name"
            :value="scale.id"
          />
        </el-select>

        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>

      <el-button type="primary" @click="exportData">
        <el-icon><Download /></el-icon>导出数据
      </el-button>
    </div>

    <!-- 测评记录列表 -->
    <el-table
      v-loading="loading"
      :data="assessmentList"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="userName" label="用户名" />
      <el-table-column prop="scaleName" label="量表名称" />
      <el-table-column prop="totalScore" label="得分" width="80" />

      <el-table-column prop="createdAt" label="创建时间" width="180">
        <template #default="{ row }">
          {{ row.createdAt ? new Date(row.createdAt).toLocaleString() : '-' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link @click="handleDetail(row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Download } from '@element-plus/icons-vue'
import request from '../../../utils/request'

const loading = ref(false)
const searchQuery = ref('')
const scaleId = ref('')
const dateRange = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const assessmentList = ref([])
const scales = ref([])

// 获取量表列表
const fetchScales = async () => {
  try {
    const res = await request.get('/admin/scales')
    if (res.code === 200) {
      scales.value = res.records
    }
  } catch (error) {
    console.error('获取量表列表失败:', error)
  }
}

// 获取测评记录列表
const fetchAssessments = async () => {
  loading.value = true
  try {
    const res = await request.get('/admin/scale/record', {
      params: {
        page: currentPage.value,
        size: pageSize.value,
        query: searchQuery.value
      }
    })
    if (res.code === 200) {
      assessmentList.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error(res.message || '获取测评记录失败')
    }
  } catch (error) {
    console.error('获取测评记录失败:', error)
    ElMessage.error('获取测评记录失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchAssessments()
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchAssessments()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchAssessments()
}

// 查看详情
const handleDetail = (row) => {
  // TODO: 跳转到详情页面
}

// 导出数据
const exportData = () => {
  // TODO: 实现导出功能
}

onMounted(() => {
  fetchScales()
  fetchAssessments()
})
</script>

<style scoped>
.assessments-manage {
  padding: 20px;
}

.table-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.left {
  display: flex;
  gap: 15px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style> 